<?php
class statistics extends core {
	public function __construct() {
		parent::__construct();
	}


	public function member() {

		$year = (isset($_GET['year']) && trim($_GET['year'])) ? trim($_GET['year']) : '';
		$month = (isset($_GET['month']) && trim($_GET['month'])) ? trim($_GET['month']) : '';
		if(!$year){
			$year = date('Y');
		}
		if(!$month){
			$month = date('m');
		}
		$premonth = $month - 1;
		$num = date("t",strtotime($year."-".$month));
		$arr = array();
		$days = array();
		for ($i=1; $i <= $num; $i++) { 
			$days[] = "'".$i."'";
		}
		$days = implode(',', $days);
		for($i=0;$i<$num;$i++){
			$dateStr = $year."-".$month."-".$i;
			$starttime = strtotime($dateStr);
			$endtime = $starttime + 86399;
			$sql = "select count(*) as num from jf_member where reg_time >= {$starttime} and reg_time <= {$endtime}";
			$a = $this->db->get_var($sql);
			$arr[] =$a;
			// $arr[] =strval($a);
		}
		$rs = implode(',', $arr);
		$this->smarty->assign("rs",$rs);
		$this->smarty->assign("year",$year);
		$this->smarty->assign("month",$month);
		$this->smarty->assign("days",$days);
		$this->display();
	}
	public function order() {

		$year = (isset($_GET['year']) && trim($_GET['year'])) ? trim($_GET['year']) : '';
		$month = (isset($_GET['month']) && trim($_GET['month'])) ? trim($_GET['month']) : '';
		if(!$year){
			$year = date('Y');
		}
		if(!$month){
			$month = date('m');
		}
		$num = date("t",strtotime($year."-".$month));
		$arr = array();
		$days = array();
		$total_price = array();
		for ($i=1; $i <= $num; $i++) { 
			$days[] = "'".$i."'";
		}
		$days = implode(',', $days);
		for($i=0;$i<$num;$i++){
			$dateStr = $year."-".$month."-".$i;
			$starttime = strtotime($dateStr);
			$endtime = $starttime + 86399;
			$sql = "select count(*) as num from jf_order where status > 0 and add_time >= {$starttime} and add_time <= {$endtime}";
			$arr[] = $this->db->get_var($sql);
			$sql = "select sum(total_price) from jf_order where status > 0 and add_time >= {$starttime} and add_time <= {$endtime}";
			$price =$this->db->get_var($sql);
			if(!$price){
				$price = '0.00';
			}
			$total_price[] = $price;
		}
		$rs = implode(',', $arr);
		$total_price = implode(',', $total_price);
		$this->smarty->assign("rs",$rs);
		$this->smarty->assign("total_price",$total_price);
		$this->smarty->assign("year",$year);
		$this->smarty->assign("month",$month);
		$this->smarty->assign("days",$days);
		$this->display();
	}
	public function sales() {
		$tol='';
		$year = (isset($_GET['year']) && trim($_GET['year'])) ? trim($_GET['year']) : '';
		$month = (isset($_GET['month']) && trim($_GET['month'])) ? trim($_GET['month']) : '';
		$sort = (isset($_GET['sort']) && trim($_GET['sort'])) ? intval($_GET['sort']) : 0;
		if($sort == 2){
			$sort = 'quantity';
		}else{
			$sort = 'price';
		}
		if(!$year){
			$year = date('Y');
		}
		if(!$month){
			$month = date('m');
		}
		$dateStr = $year."-".$month;
		$starttime = strtotime($dateStr);
		$months = $month + 1;
		$endStr = $year."-".$months;
		$endtime = strtotime($endStr);
		$endtime = $endtime - 1;
		//分页
		$this->setpages(array('year'=>$year, 'month'=>$month, 'sort'=>$sort));
		$sql = "SELECT goods_name,goods_id,sum(price*quantity) as price,sum(quantity) as quantity FROM jf_order_goods WHERE order_id in (SELECT id FROM jf_order o WHERE o.status > 0 and  o.add_time >= {$starttime} 
		       and o.add_time <={$endtime}) group by goods_id order by {$sort} desc";
		$tol = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign("row",$tol);
		$this->smarty->assign("page",$nav);
		$this->smarty->assign("year",$year);
		$this->smarty->assign("month",$month);

		$this->display();
	}
}
